package ui.activity.mine;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;

import net.ck.hzq.R;
import net.ck.hzq.TobuyApplication;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.concurrent.TimeUnit;

import Utils.SharedPrefStore;
import base.BaseAct;
import base.BaseBiz;
import butterknife.BindView;
import butterknife.ButterKnife;
import coom.Constants;
import coom.MyType;
import coom.RetrofitHelp;
import event.CloseActEvent;
import event.NetErrorEvent;
import ui.activity.login.LoginAct;
import ui.activity.profit.biz.ProfitBiz;
import util.AlertUtils;
import widget.alertview.AlertView;


/**
 * 设置界面
 * Created by chenjian on 2017/8/4.
 */

public class SettingAct extends BaseAct {


    ProfitBiz biz;
    @BindView(R.id.setting_changepassword)
    TextView changePwd;
    @BindView(R.id.setting_logout)
    TextView loginOut;
    @BindView(R.id.setting_pay_password)
    TextView payPassword;
    @BindView(R.id.setting_change_phone)
    TextView changePhone;

    @Override
    protected void setRoot() {
        setContentView(R.layout.activity_setting);
    }

    @Override
    protected void setup() {
        setToolBar(getResources().getString(R.string.setting), "");
    }

    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        biz = new ProfitBiz();
        RxView.clicks(changePwd)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> startAct(ChangePwdAct.class, null));
        RxView.clicks(loginOut)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> LogOut());
        RxView.clicks(payPassword)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> startAct(PayPwdAct.class, null));
        RxView.clicks(changePhone)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void ->
                {
                    if (Constants.NeedAuth) {
                        showMsg("未实名认证的用户无法更换手机号码");
                        return;
                    }
                    startAct(ChangePhoneAct.class, null);
                });
    }

    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    private void LogOut() {
        AlertView checkAlert = AlertUtils.showInfo(SettingAct.this, new AlertUtils.AlertViewControl() {
            @Override
            public void onItemClickListener(int positions) {
                if (positions == 0) {
                    biz.logout(new BaseBiz.Callback<String>() {
                        @Override
                        public void onSuccess(String s) {
                        }

                        @Override
                        public void onFailure(String s) {

                        }
                    });
                    RetrofitHelp.logout();
                    startAct(LoginAct.class, null);
                    EventBus.getDefault().post(new CloseActEvent(MyType.Logout, ""));
                    TobuyApplication.token = "";
                    SharedPrefStore store = new SharedPrefStore();
                    store.putString("login_password", "");
                    store.putString("messageId", "");
                    store.putString("messageSaveContent", "");

                    finishAct();
                }
            }
        }, "提示", "是否确定退出？");
        checkAlert.show();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCloseActEvent(CloseActEvent event) {
        if (event.getType() == MyType.Logout) {
            finishAct();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
